package com.wei.web.demo.biz.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.wei.web.demo.biz.service.BizService;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author buhuan.wang
 * @since 2023/1/20
 */
@Configuration
public class SentinelConfig {
    @PostConstruct
    public void init() {
        initBizServiceCreate();
    }

    void initBizServiceCreate() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource(BizService.CREATE_SENTINEL_RESOURCE);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
